Passed
Pull Request — master (#223)
by Daniel
01:36
created

classValidText   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 19
dl 0
loc 28
rs 10
c 0
b 0
f 0
1
interface InputEvent extends Event {
2
  target: HTMLInputElement;
3
}
4
5
interface SubmitButton extends HTMLElement {
6
  removeAttribute(name: string): void;
7
  setAttribute(name: string, value: string): void;
8
}
9
10
/**
11
 * checkValidText is where we check if the text input is valid
12
 * If it is, we enable the submit button
13
 */
14
const checkValidText = (event: InputEvent): void => {
15
  const bilInformasjon = event.target.value;
16
  const submitButton = window.document.getElementById(
17
    "submitButton"
18
  ) as SubmitButton;
19
20
  const letters = /[A-Z]{2}\d{5}/gi;
21
  const bilInformasjonMatchesFormat = letters.test(bilInformasjon);
22
23
  if (
24
    bilInformasjonMatchesFormat &&
25
    bilInformasjon !== undefined &&
26
    bilInformasjon.length === 7
27
  ) {
28
    submitButton.removeAttribute("disabled");
29
  } else {
30
    submitButton.setAttribute("disabled", "true");
31
  }
32
};
33
34
export default checkValidText;
35